package twoDayMoreThread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockExample {
    private final Lock lock=new ReentrantLock();
    private int count;

    public void increment(){
        lock.lock();
        try {
            count++;
        } finally {
            lock.unlock();
        }
    }
    public int getCount(){
        lock.lock();
        try {
            return count;
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        LockExample lockExample = new LockExample();
        //通过runnable多态指向一个工作的多线程的
        Runnable task = ()->{
            for (int i = 0; i < 1000; i++) {
                lockExample.increment();
                System.out.println("lockExample.getCount() = " + lockExample.getCount());
            }
        };
      task.run();//启动线程
    }
}
